﻿@using DevExpress.Blazor
@using DevExpress.ExpressApp.Blazor.Components
@using EasyXaf.LowCode.WorkflowEditors.Models
@using EasyXaf.LowCode.WorkflowEditors.Models.Activities

<div class="@ActivityNodeClassName" style="@ActivityNodeStyle">
    <div class="node-content-container">
        <div class="node-content node-outline" outline-id="outline-@(Activity.Id)">
            @if (WorkflowMode == WorkflowMode.Outline)
            {
                <div class="node-outline-content" />
            }
            else
            {
                <DynamicComponent @ref="ActivityComponent"
                                  Type="ActivityComponentType"
                                  Parameters="ActivityComponentParameters" />
            }
        </div>
        @if (!IsReadOnly)
        {
            if (Activity.ValidationResults.Any())
            {
                <NodeAction CssClass="activity-validation-result-action" IconName="Actions_Info" />
            }

            if (CanDelete)
            {
                <NodeAction CssClass="activity-delete-action hidden-action"
                            Tooltip="删除"
                            IconName="Actions_DeleteCircled"
                            Click="Delete" />

                if (CanCopy)
                {
                    <NodeAction CssClass="activity-cut-action hidden-action"
                                Tooltip="剪切"
                                IconName="Actions_Cut"
                                Click="Cut" />
                }
            }

            if (CanMoveUp)
            {
                <NodeAction CssClass="activity-move-up-action hidden-action"
                            Tooltip="向上移动"
                            IconName="Actions_Arrow3Up"
                            IconColor="#7F8C8D"
                            Click="MoveUp" />
            }

            if (CanMoveDown)
            {
                <NodeAction CssClass="activity-move-down-action hidden-action"
                            Tooltip="向下移动"
                            IconName="Actions_Arrow3Down"
                            IconColor="#7F8C8D"
                            Click="MoveDown" />
            }
        }
    </div>
    <div class="node-line">
        <div class="v-line"></div>
        @if (Activity is IEndActivity)
        {
            if (Activity.Sequence is IBranch && WorkflowMode != WorkflowMode.Outline)
            {
                <NodeAction CssClass="end-action" IsReadOnly="true" IconName="Actions_Forbid" />
            }
        }
        else
        {
            if (Activity is IBranchesActivity branchesActivity && WorkflowMode != WorkflowMode.Outline)
            {
                if (branchesActivity.IsCollapse)
                {
                    <NodeAction CssClass="collapse-branches-action"
                                IconSize="10"
                                Tooltip="展开"
                                IconName="ExpandSubordinates"
                                Click="CollapseActionClicked" />
                }
                else
                {
                    <NodeAction CssClass="collapse-branches-action"
                                IconSize="10"
                                Tooltip="折叠"
                                IconName="CollapseSubordinates"
                                Click="CollapseActionClicked" />
                }
            }

            if (!IsReadOnly && VisibleAddAction)
            {
                <NodeAction CssClass="add-activity-action" Tooltip="添加活动..." IconName="Actions_AddCircled" Click="AddActivityClicked" />
            }
        }
    </div>
</div>

<DxContextMenu @ref="ActivityContextMenu">
    <Items>
        @if (Activity.AllowMove)
        {
            <DxContextMenuItem Text="置顶" CssClass="activity-context-menu-item" Enabled="CanMoveTop" IconUrl="IImageService/Actions_Arrow2Up" Click="MoveTop" />
            <DxContextMenuItem Text="上移" CssClass="activity-context-menu-item" Enabled="CanMoveUp" IconUrl="IImageService/Actions_Arrow4Up" Click="MoveUp" />
            <DxContextMenuItem Text="下移" CssClass="activity-context-menu-item" Enabled="CanMoveDown" IconUrl="IImageService/Actions_Arrow4Down" Click="MoveDown" />
            <DxContextMenuItem Text="置底" CssClass="activity-context-menu-item" Enabled="CanMoveBottom" IconUrl="IImageService/Actions_Arrow2Down" Click="MoveBottom" />
        }
        @if (Activity.AllowScale)
        {
            <DxContextMenuItem Text="大小" CssClass="activity-context-menu-item" Enabled="!IsReadOnly" IconUrl="IImageService/AutoSize" BeginGroup="true">
                <Items>
                    @for (var i = 1.0; i <= 8; i += (i >= 5 ? 1 : 0.5))
                    {
                        var scale = i;
                        var menuText = $"{scale}倍";
                        var iconUrl = string.Empty;
                        if (Activity.ViewMode == NodeViewMode.Title)
                        {
                            if (Activity.TitleViewScale == scale)
                            {
                                iconUrl = "IImageService/Actions_Check";
                            }
                        }
                        else
                        {
                            if (Activity.DetailViewScale == scale)
                            {
                                iconUrl = "IImageService/Actions_Check";
                            }
                        }
                        <DxContextMenuItem Text="@menuText" CssClass="activity-context-menu-item" IconUrl="@iconUrl" Click="()=>SetScale(scale)" />
                    }
                </Items>
            </DxContextMenuItem>
        }
        @if (AllowAddingComment)
        {
            <DxContextMenuItem Text="添加备注" CssClass="activity-context-menu-item" Enabled="!IsReadOnly" IconUrl="IImageService/Actions_Comment" BeginGroup="true" Click="AddComment" />
        }
        @if (AllowCopy)
        {
            <DxContextMenuItem Text="复制" CssClass="activity-context-menu-item" Enabled="!IsReadOnly" IconUrl="IImageService/Actions_Copy" BeginGroup="true" Click="Copy" />
            if (AllowDelete)
            {
                <DxContextMenuItem Text="剪切" CssClass="activity-context-menu-item" Enabled="!IsReadOnly" IconUrl="IImageService/Actions_Cut" Click="Cut" />
            }
        }
        @if (AllowPaste)
        {
            <DxContextMenuItem Text="粘贴" CssClass="activity-context-menu-item" Enabled="CanPaste" IconUrl="IImageService/Action_Paste" Click="Paste" />
        }
        @if (AllowDelete)
        {
            <DxContextMenuItem Text="删除" CssClass="activity-context-menu-item" Enabled="!IsReadOnly" IconUrl="IImageService/Actions_Delete" BeginGroup="true" Click="Delete" />
        }
        @if (Activity.AllowValidate)
        {
            <DxContextMenuItem Text="验证" CssClass="activity-context-menu-item" Enabled="!IsReadOnly" IconUrl="IImageService/BO_Validation" BeginGroup="true" Click="Validate" />
        }
    </Items>
</DxContextMenu>

<style>
    .activity-context-menu-item img {
        width: 16px;
        height: 16px;
    }
</style>
